<template>
  <div class="resource">
    <el-card shadow="hover">
      <div style="margin-bottom: 10px;">
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <el-button
          style="margin-left: 20px;"
          @click="search"
          icon="el-icon-search"
          type="cyan"
          size="small"
          >搜索</el-button
        >
        <el-button
          @click="cancelData"
          icon="el-icon-refresh"
          size="small"
          >重置</el-button
        >
      </div>
      <el-row :gutter="20">
        <el-form
          ref="params"
          :model="params"
          label-width="100px"
          size="small"
          class="searchForm"
        >
          <el-col :span="6">
            <el-form-item label="老师姓名:" prop="teacherName">
              <el-input
                v-model="params.userName"
                size="small"
                clearable
                @clear="search"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="老师电话:" prop="phone">
              <el-input
                v-model="params.phonenumber"
                size="small"
                clearable
                @clear="search"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </el-card>

    <div class="container">

      <!-- <el-card shadow="hover">
        <h4 style="margin-bottom: 10px">老师列表</h4> -->
      <el-table
        v-loading="loading"
        :data="tableData"
        ref="multipleTable"
      >
        <el-table-column label="老师名称" min-width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.userName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="phonenumber"
          label="老师电话"
          align="center"
          min-width="150"
        ></el-table-column>
        <el-table-column label="创建时间" align="center" min-width="160">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <!-- <el-table-column
          label="操作"
          min-width="200"
          align="center"
          fixed="right"
          v-if="checkRole(['manager','admin'])"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>
      <!-- </el-card> -->
      <div class="pagination">
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="params.pageNum"
          :limit.sync="params.pageSize"
          @pagination="getData00"
        />
      </div>
    </div>
    <!-- 新增编辑 -->
    <el-dialog :title="title666" :visible.sync="dialogVisible" width="50%">
      <el-form
        :rules="rules2"
        ref="formData"
        :model="formData"
        label-width="120px"
        size="small"
        class="form-box"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="老师名称" prop="teacherName">
              <el-input v-model="formData.teacherName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="formData.phone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="老师角色" prop="genre">
              <el-select
                v-model="formData.genre"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in roleList"
                  :key="item.id"
                  :label="item.role"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>           
          </el-col>
           <el-col :span="12">
            <el-form-item label="选择标签" prop="labels">
              <el-select
                v-model="formData.labels"
                filterable
                placeholder="请选择"
                clearable
                multiple
              >
                <el-option
                  v-for="item in labelList"
                  :key="item.id"
                  :label="item.labelName"
                  :value="item.labelName"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="业务范围" prop="address">
               <el-cascader
               separator="-"
                v-model="formData.address"
                filterable
                :options="options"
                :props="{ expandTrigger: 'hover',multiple: true}"
                @change="address_change"
                clearable></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="老师头像" prop="pic">
              <el-upload
                class="avatar-uploader"
                :action="upImage + '/cms/company/upload'"
                :show-file-list="false"
                :headers="headers"
                :on-success="handleAvatarSuccess"
              >
                <img
                  v-if="formData.pic"
                  :src="showUrl + formData.pic"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="老师介绍" prop="desc">
              <el-input
                type="textarea"
                :rows="6"
                v-model="formData.desc"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button @click="submitForm()" type="primary">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
import {
  teacherLabelList,
  teacherList,
  teacherList1,
  teacherList_add,
  teacherList_update,
  teacherList_delete,
  city
} from "@/api/teacher/teacherAds";
import { getToken } from "@/utils/auth";
export default {
  components: {},
  props: [],
  data() {
    return {
      options:[],
      // 删除专用
      multiple: true,
      ids: [],
      // 上传专用
      headers: { Authorization: "Bearer " + getToken() },
      upImage: process.env.VUE_APP_BASE_API,
      labelList: [],
      cityList: [],
      title666: "",
      rules2: {
        phone: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
        teacherName: [
          { required: true, message: "请输入老师名称", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请输入所在地址", trigger: "blur" },
        ],
        pic: [{ required: true, message: "请上传头像", trigger: "blur" }],
        labels: [{ required: true, message: "请选择", trigger: "blur" }],
        genre: [{ required: true, message: '请选择老师角色', trigger: "blur"  }]
      },
      input: "",
      openFlag: true,
      loading: true,
      //数据
      tableData: [],
      params: {
        pageNum: 1,
        pageSize: 10,
        isTeacher:'1',
        userName:undefined,
        phonenumber:undefined
      },
      pageTotal: 0,
      dialogVisible: false, // 是否展示编辑页面
      delList: [],
      // 新增/编辑数据
      formData: {
        pic: "",
        labels: [],
      },
      customer: {
        customerId: "",
      },
      paramsFlag: 0,
      editFlags: false,
      roleList: [
        { id: '1', role: '咨询老师' },
        { id: '2', role: '授课老师' }
      ]
    };
  },
  filters: {
    // 标签数组过滤器
    // toString(val) {
    //   let val6 = [];
    //   val.forEach((item) => {
    //     val6.push(item.name);
    //   });
    //   return val6.toString();
    // },
  },
  computed: {},
  created() {
    this.getData00();
    this.getLabels();
  },
  mounted() {
    this.city()
  },
  watch: {},
  methods: {
    checkPermi, checkRole,
    address_change(e){
      console.log(e)
      
    },
    city(){
      city().then((res)=>{
        if(res.code==200){
          console.log(res);
          this.options=res.data;
        }
      })
    },
    handleAvatarSuccess(res, file) {
      if (res.code == 200) {
        this.formData.pic = res.data;
        // console.log(this.formData.pic);
        this.$message.success(`上传成功`);
      }
    },
    getLabels() {
      teacherLabelList({
        pageNum: 1,
        pageSize: 99999,
      }).then((response) => {
        this.labelList = response.rows;
      });
    },
    //   获取创建时间
    createTime(e) {
      console.log(e);
    },
    // 新增修改的方法
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.formData.labels = this.formData.labels.join(',');
          this.formData.address = JSON.stringify(this.formData.address);
          if (this.title666 == "新增") {
            teacherList_add(this.formData).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success(`新增成功`);
                this.dialogVisible = false;
                this.getData00();
              }
            });
          } else {
            teacherList_update(this.formData).then((res) => {
              if (res.code == 200) {
                this.$message.success(`修改成功`);
                this.dialogVisible = false;
                this.uploading = false;
                this.getData00();
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 打开搜索
    openSearch() {
      this.openFlag = !this.openFlag;
    },
    // 重置搜索框
    cancelData() {
      // this.resetForm("params");
      this.params={
        pageNum: 1,
        pageSize: 10,
        isTeacher:'1',
        userName:undefined,
        phonenumber:undefined
      }
      this.getData00();
    },
    handleAdd() {
      this.formData = {
        pic: "",
        labels: [],
      };
      this.dialogVisible = true;
      this.title666 = "新增";
    },
    handleEdit(index, row) {
      this.formData = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
      this.title666 = "编辑";
      this.formData.labels = this.formData.labels.split(',');
      this.formData.address = JSON.parse(this.formData.address);
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    // 删除操作
    handleSelectionChange(val) {
      this.ids = val.map((item) => item.id);
      this.single = val.length != 1;
      this.multiple = !val.length;
    },
    // 批量删除
    handleDelete(row) {
      // console.log(row.id);
      const id = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return teacherList_delete(id);
        })
        .then(() => {
          this.getData00();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    //获取数据
    getData00() {
      let data = this.params;
      teacherList1(data)
        .then((res) => {
          if (res.code == 200) {
            this.tableData = res.rows;
            this.pageTotal = res.total;
            this.loading = false;
          }
        })
        .catch(() => {
          console.log("error submit!!");
          return false;
        });
    },
    // 触发搜索按钮
    search() {
      this.$set(this.params, "pageNum", 1);
      this.paramsFlag = 0;
      this.getData00();
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.params, "pageNum", val);
      this.getData00();
    },
  },
};
</script>

<style scoped>
.resource {
  padding: 30px;
}
.container {
  margin-top: 20px;
}
.handle-box {
  margin-bottom: 10px;
}

.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.yellow {
  color: #f57506;
}
.green {
  color: green;
  cursor: pointer;
}
.blue {
  color: #409eff;
  cursor: pointer;
  text-decoration: underline;
}
.mr10 {
  margin-right: 10px;
}

.searchForm .el-form-item--mini.el-form-item,
.searchForm .el-form-item--small.el-form-item {
  margin-bottom: 1px;
}
.study_pic {
  display: block;
  width: 60px;
  height: 60px;
  margin: auto;
  border-radius: 50%;
}
.avatar-uploader .el-upload {
  width: 148px;
  height: 148px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
  border: 1px dashed #d9d9d9;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>
